package org.grayrabbit.cms.plugin.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.grayrabbit.cms.plugin.ModuleDescriptor;
import org.grayrabbit.cms.plugin.Plugin;
import org.grayrabbit.cms.plugin.PluginInformation;
import org.grayrabbit.cms.plugin.PluginPermission;
import org.grayrabbit.cms.plugin.PluginWebMenuItem;
import org.grayrabbit.cms.plugin.Resourced;
import org.grayrabbit.cms.plugin.Resources;
import org.grayrabbit.cms.plugin.elements.ResourceLocation;


/**
 * @author sys53
 * @version 1.0
 * @created 06-一月-2012 16:57:39
 */
public abstract class AbstractPlugin implements Plugin {

	public Resourced resourced;
	private Class<?> installer;
	private List<PluginPermission>permissions;
	private boolean systemPlugin = false;
	private String i18nNameKey;
	private String name;
	private String key;
	private ResourceLocation resourceLocation;
	private boolean deleteable;
	private boolean enabled;
	private List<PluginWebMenuItem> pluginWebMenuItems;
	private PluginInformation pluginInformation;
	private Date dateLoaded = new Date();
	private Map<String,ModuleDescriptor> modules = new LinkedHashMap<String,ModuleDescriptor>();
	 private Resourced resources = Resources.EMPTY_RESOURCES;
	public Date getDateLoaded() {
		return dateLoaded;
	}


	public AbstractPlugin(){
	}


	public void close(){

	}




	public ResourceLocation getResourceLocation() {
		return resourceLocation;
	}
	
	public void setResources(Resourced resources)
	  {
	    this.resources = (resources != null ? resources : Resources.EMPTY_RESOURCES);
	  }

	public ResourceLocation getResourceLocation(String type, String name)
	  {
	    return this.resources.getResourceLocation(type, name);
	  }
	
	public boolean isEnabled() {
		return enabled;
	}


	public boolean isDefaultEnabled(){
		return false;
	}
	
	public boolean isSystemPlugin(){
		return systemPlugin;
	}

	public boolean isUninstallable(){
		return false;
	}


	public String getI18nNameKey() {
		return i18nNameKey;
	}


	public void setI18nNameKey(String i18nNameKey) {
		this.i18nNameKey = i18nNameKey;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getKey() {
		return key;
	}


	public void setKey(String key) {
		this.key = key;
	}


	public boolean isDeleteable() {
		return deleteable;
	}


	public void setDeleteable(boolean deleteable) {
		this.deleteable = deleteable;
	}


	public List<PluginWebMenuItem> getPluginWebMenuItems() {
		return pluginWebMenuItems;
	}


	public void setPluginWebMenuItems(List<PluginWebMenuItem> pluginWebMenuItems) {
		this.pluginWebMenuItems = pluginWebMenuItems;
	}
	
	public void addWebMenuItem(PluginWebMenuItem webMenuItem){
		if(this.pluginWebMenuItems==null){
			this.pluginWebMenuItems = new ArrayList<PluginWebMenuItem>();
		}
		pluginWebMenuItems.add(webMenuItem);
	}

	public void setResourceLocation(ResourceLocation resourceLocation) {
		this.resourceLocation = resourceLocation;
	}
	
	


	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}


	public List<PluginPermission> getPermissions() {
		return permissions;
	}


	public void setPermissions(List<PluginPermission> permissions) {
		this.permissions = permissions;
	}


	public Class<?> getInstaller() {
		return installer;
	}


	public void setInstaller(Class<?> installer) {
		this.installer = installer;
		
	}

	public void addPluginPermission(PluginPermission p3) {
		if(permissions==null){
			permissions = new ArrayList<PluginPermission>();
		}
		permissions.add(p3);
		
	}
	
	public Resourced getResourced() {
		return resourced;
	}


	public void setResourced(Resourced resourced) {
		this.resourced = resourced;
	}


	public void setResources(Resources fromXml) {
		this.resourced = fromXml;
	}

	public void setSystemPlugin(boolean b) {
		this.systemPlugin = b;
	}


	public PluginInformation getPluginInformation() {
		return pluginInformation;
	}


	public void setPluginInformation(PluginInformation pluginInformation) {
		this.pluginInformation = pluginInformation;
	}
	
	public ModuleDescriptor getModuleDescriptor(String moduleKey) {
		return this.modules.get(moduleKey);
	}
	public void addModuleDescriptor(ModuleDescriptor moduleDescriptor)
	  {
	    this.modules.put(moduleDescriptor.getKey(), moduleDescriptor);
	  }
}